window.onload = function () {
    document.getElementById("technological-backlog").className = "add_class";
    document.getElementById("technological").className = "add-nav-active";
    station_list();
};



// 加载岗位列表
function station_list(sent_str="", page=1) {
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/technological/backlog.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send(sent_str+"&page="+String(page));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                var JsonDate = JSON.parse(xhr.responseText);
                create_table(JsonDate["count_num"], JsonDate["data"]);
                pagenum(JsonDate["count_num"], page);
                document.getElementById("loading").style.display = "none";
            }
        }
    };
}

function create_table(count_num, json_data) {
    var t_table = document.getElementById("t_table");
    deltr();
    if (json_data.length === 0){
        document.getElementById("img-display").style.display = "inline-block";
    }else {
        document.getElementById("img-display").style.display = "none";
    }
    for (var i=0; i<json_data.length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");//"+json_data[i]["form_id"]+"', '"+json_data[i]["id"]+"'
        td.innerHTML = i+1;td.style.textAlign = "center";
        td1.innerHTML = json_data[i]["name"];td1.title = json_data[i]["name"];td1.style.textAlign = "center";
        td2.innerHTML = json_data[i]["form_name"];td2.title = json_data[i]["form_name"];td2.style.textAlign = "center";
        td3.innerHTML = json_data[i]["apply_time"];td3.title = json_data[i]["apply_time"];td3.style.textAlign = "center";
        if(json_data[i]["form_name"]==='用车申请'){
            td4.innerHTML = "<a href='/administrative/examination-vehicle.html?id="+json_data[i]["id"]+"' target=''>审批</a>";td4.style.textAlign = "center";
        }
        else if(json_data[i]["form_name"]==='会议申请'){
            td4.innerHTML = "<a href='/administrative/meeting_detail.html?MeetingInfo_id="+json_data[i]["form_id"]+"&application_id="+json_data[i]["id"]+"&type=my_backlog"+"' target=''>审批</a>";td4.style.textAlign = "center";
        }
        else{
            td4.innerHTML = "<a href='/technological/approve.html?form_id="+json_data[i]["form_id"]+"&flow_id="+json_data[i]["id"]+"' target='_blank'>审批</a> | <a href='#' data-toggle=\"modal\" data-target=\".bd-example-modal-xl\" onclick='entrust("+json_data[i]["form_id"]+", "+ json_data[i]["id"]+", \""+json_data[i]["form_name"]+"\")'>委托</a>";td4.style.textAlign = "center";

        }
        tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);
        t_table.appendChild(tr);
    }

}

// 删除列表
function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}

// 删除列表
function deltr1(){
    var tb = document.getElementById('t_table2');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}


function pagenum(dataTotals=0, page=1) {
    new myPagination({
        id: 'pagination',
        curPage: page, //初始页码
        pageAmount: 20,  //每页多少条
        dataTotal: dataTotals, //总共多少条数据
        pageTotal:  Math.ceil(parseInt(dataTotals, 10)/20), //总页数
        pageSize: 5, //可选,分页个数
        showPageTotalFlag:true, //是否显示数据统计
        showSkipInputFlag:true, //是否支持跳转
        getPage: function (page) {
            station_list(sent_str='', page);
            document.getElementById("check_all").checked = false;
        }
    });
}


// 流程委托弹框
function entrust(form_id, flow_id, form_name) {
    document.getElementById("model-affirm").setAttribute("onclick", "submit_consignor("+form_id+", "+flow_id+", '"+form_name+"')");

    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/technological/delegation", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                var JsonDate = JSON.parse(xhr.responseText);
                window.personnel_list = JsonDate["data"];
                var department_list = document.getElementById("department_list");
                department_list.innerHTML = "";
                var ul_tag = document.createElement("ul");
                for (var i=0; i<JsonDate["branch_list"].length; i++) {
                    var li_tag = document.createElement("li");
                    li_tag.innerHTML = JsonDate["branch_list"][i];li_tag.setAttribute("onclick", "choice_peopel('"+JsonDate["branch_list"][i]+"')");li_tag.className="li-class";
                    ul_tag.appendChild(li_tag);
                    department_list.appendChild(ul_tag);
                }
            }
        }
    };
}


// 创建右侧选择人员列表
function choice_peopel(choice_key) {
    var personnel_list = window.personnel_list;
    document.getElementById("img-display1").style.display = "none";
    var t_table = document.getElementById("t_table2");
    deltr1();
    for (var i=0; i<personnel_list[choice_key].length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");
        td.innerHTML = "<input id='"+personnel_list[choice_key][i]+"' class='check_box' type=\"checkbox\" onclick='choice_consignor(this.id)'>";td.style.textAlign = "center";
        td1.innerHTML = i+1;td1.style.textAlign = "center";
        td2.innerHTML = personnel_list[choice_key][i];td2.title =personnel_list[choice_key][i];td2.style.textAlign = "center";

        tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);
        t_table.appendChild(tr);
    }
}


// 选择委托人
function choice_consignor(ele_id) {
    check_more_one_not(ele_id);
    document.getElementById("checked-label").innerHTML = ele_id;
}

// checkBox不允许多选
function check_more_one_not(ele_id) {
    var check_box_list = document.getElementsByClassName("check_box");
    for (var i=0; i<check_box_list.length; i++){
        if (String(check_box_list[i].id) !== String(ele_id)){
            document.getElementById(check_box_list[i].id).checked = false;
        }else {
            document.getElementById(check_box_list[i].id).checked = true;
        }
    }
}


// 提交委托人
function submit_consignor(form_id, flow_id, form_name) {
    var mandatary = document.getElementById("checked-label").innerText;        // 被委托人
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/technological/entrust.html", true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send("form_name="+form_name+"&form_id="+form_id+"&flow_id="+flow_id+"&mandatary="+mandatary);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                var JsonDate = JSON.parse(xhr.responseText);
                if (JsonDate["code"] === 200) {
                    alert("流程委托成功");
                }
            }
        }
    };
}
